<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model.trim="dataForm.keywords" placeholder="关建词" clearable style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item label="起始时间">
        <el-date-picker
          v-model="dataForm.startTime"
          type="datetime"
          value-format = "yyyy-MM-dd HH:mm:ss"
          placeholder="起始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间">
        <el-date-picker
          v-model="dataForm.endTime"
          type="datetime"
          value-format = "yyyy-MM-dd HH:mm:ss"
          placeholder="结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="
          dataForm.keywords = ''
          dataForm.startTime = ''
          dataForm.endTime = ''
          getDataList()">重置</el-button>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
      <br/>
      <el-form-item label="">
        <el-select v-model="batchValue" placeholder="批量启用">
          <el-option key="batchEnable" label="批量启用" value="batchEnable"></el-option>
          <el-option key="batchClose" label="批量关闭" value="batchClose"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain @click="batch(batchValue)" :disabled="dataListSelections.length <= 0">批量操作</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      :header-cell-style="tableHeaderColor"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="1">
      </el-table-column>
      <el-table-column
          label="序号"
          type="index"
          width="58"
          align="center">
        </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="ID"
        v-if="show">
      </el-table-column>
      <el-table-column
        prop="keyWord"
        header-align="center"
        align="center"
        label="关建词">
      </el-table-column>
      <el-table-column
        prop="remarks"
        header-align="center"
        align="center"
        label="备注">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="state"
        header-align="center"
        align="center"
        label="是否启用"
        width="100">
        <template slot-scope="scope">
          <el-switch
            style="margin-left: 10px;margin-bottom: 5px;"
            v-model="scope.row.state"
            :active-value= 1
            :inactive-value= 0
            @change="enableChange(dataList[scope.$index].id, scope.row.state)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="250"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="selectHandle(scope.row.id)">查看</el-button>
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">编辑</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-update-shoppingegg v-if="addOrUpdateVisible" ref="addUpdateShoppingegg" @refreshDataList="getDataList"/>
    <select-shopping-egg v-if="selectVisible" ref="selectShoppingEgg" @refreshDataList="getDataList"/>
  </div>
</template>

<script>
  import AddUpdateShoppingegg from './add-update-shoppingegg'
  import SelectShoppingEgg from './select-shoppingegg'
  export default {
    data () {
      return {
        dataForm: {
          keywords: '',
          startTime: '',
          endTime: ''
        },
        keyWordList: [],
        show: false,
        batchValue: '',
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        selectVisible: false,
        colors: ['#99A9BF', '#F7BA2A', '#FF9900']
      }
    },
    components: {
      AddUpdateShoppingegg,
      SelectShoppingEgg
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        debugger
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl(`/egg/queryGoodsEggList`),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keywords': this.dataForm.keywords,
            'startTime': this.dataForm.startTime,
            'endTime': this.dataForm.endTime
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.keyWordList = []
        if (this.dataList.length > 0) {
          for (var i = 0; i < this.dataList.length; i++) {
            this.keyWordList.push(this.dataList[i].keyWord)
          }
        }
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addUpdateShoppingegg.init(this.keyWordList, id)
        })
      },
      // 查看
      selectHandle (id) {
        this.selectVisible = true
        this.$nextTick(() => {
          this.$refs.selectShoppingEgg.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        debugger
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/egg/removeEggByIds'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      // 批量启用
      batch (batchValue) {
        debugger
        var ids = this.dataListSelections.map(item => {
          return item.id
        })
        console.log(ids)
        if (ids.length === 0) {
          this.$message.error('请选择数据')
          return
        }
        this.$confirm(`确定对【id=${ids.join(',')}】进行状态操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/egg/modifyEggStateByIds/${batchValue}`),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      // 改变启用状态
      enableChange (id, state) {
        this.$confirm(`确定对[id=${id}]进行[状态更新]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/egg/modifyEggStateById`),
            method: 'post',
            data: this.$http.adornData({
              'id': id || undefined,
              'state': state ? 1 : 0
            }, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
        this.getDataList()
      },
      // 修改table header的背景色
      tableHeaderColor ({ row, column, rowIndex, columnIndex }) {
        if (rowIndex === 0) {
          return 'background-color: #17B3A3;color: #303133;font-weight: 500;'
        }
      }
    }
  }
</script>
